package com.game_info.controller;

import java.util.List;

import com.game_info.service.UsersService;
import com.game_info.utils.AjaxResult;
import com.game_info.vo.Users;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * 用户表Controller
 *
 * @author ttj
 * @date 2020-04-14
 */
@Controller
@RequestMapping("/users")
public class UsersController {

    @Autowired
    private UsersService usersService;


    /**
     * 查询用户列表
     * (能根据用户名+其他列模糊查询)
     */
    @PostMapping("/list")
    @ResponseBody
    public AjaxResult list(Users users) {
        List<Users> list = usersService.selectUsersList(users);
        return AjaxResult.put(list, "执行用户列表查询方法执行成功");
    }


    /**
     * 新增用户
     */
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult add(Users users) {
        return AjaxResult.put(usersService.insertUsers(users), "新增用户方法成功");
    }


    /**
     * 分页查询
     *
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("/selectUsersLimit.do")
    @ResponseBody
    public AjaxResult selectUsersLimit(Integer page, @RequestParam(value = "limit", required = false) Integer limit) {
        if (limit == null) {
            limit = 5;//默认分页数据量5
        }
        //入参小于0的情况.
        if (page <= 0) {
            page = 1;
        }
        Integer max=usersService.getUsersCount();//获取留言总数
        List<Users> list=usersService.selectUsersLimit((page - 1) * limit, limit);
        return AjaxResult.put(list,"分页查询方法执行成功",max);
    }

    /**
     * 获取全部用户总数
     *
     * @return
     */
    @GetMapping("/getUsersCount.do")
    @ResponseBody
    public Integer getUsersCount() {
        return usersService.getUsersCount();
    }

    /**
     * 根据关键词模糊查询全部字段信息
     *
     * @param keyword
     * @return
     */
    @GetMapping("/selectLikeKeyword.do")
    @ResponseBody
    public List<Users> selectLikeKeyword(String keyword) {
        return usersService.selectLikeKeyword(keyword);
    }

    /**
     * 修改用户
     */
    @PostMapping("/edit.do")
    @ResponseBody
    public AjaxResult edit(@RequestBody Users users) {
        return AjaxResult.put(usersService.updateUsers(users), "修改用户方法执行成功");
    }

    /**
     * 删除用户
     */
    @PostMapping("/remove.do")
    @ResponseBody
    public AjaxResult remove(@RequestBody Integer uId) {
        return AjaxResult.put(usersService.deleteUsersById(uId), "执行删除用户方法成功");
    }
}
